﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Autobiography.UI
{
    /// <summary>
    /// Interaction logic for History.xaml
    /// </summary>
    public partial class History : UserControl, Microsoft.WebMatrix.Extensibility.IDialogContent
    {
        public event EventHandler<EventArgs> CloseDialog;

        public Model.FileEntry SelectedItem { get; private set; }

        public History(IEnumerable<Model.FileEntry> Entries)
        {
            InitializeComponent();

            TopLevelListBox.ItemsSource = Entries
                .OrderByDescending(x => x.Modified)
                .GroupBy(Design.DateGrouping.grouping)
                .OrderBy(Design.DateGrouping.sorting);
        }

        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //this.SelectedItem = (sender as TextBlock).GetVisualAncestor<ListBox>().SelectedItem as Model.FileEntry;
            //var list = (sender as TextBlock).GetVisualAncestors();
            //var host = (sender as TextBlock).GetVisualAncestor<Microsoft.WebMatrix.Extensibility.>();
            if (e.ClickCount == 2)
            {
                CloseDialog(this, EventArgs.Empty);
            }
        }

        private void SubListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //var list = (sender as TextBlock).GetVisualAncestors();
            this.SelectedItem = e.AddedItems.Cast<Model.FileEntry>().FirstOrDefault();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedItem = null;
            CloseDialog(this, EventArgs.Empty);
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.SelectedItem != null)
            {
                CloseDialog(this, EventArgs.Empty);
            }
        }

        private void TopLevelListBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ListBox box = sender as ListBox;
            box.Width = dock.ActualWidth;
        }

        /*
        event EventHandler<EventArgs> Microsoft.WebMatrix.Extensibility.IDialogContent.CloseDialog
        {
            add { }
            remove { }
        }
        */
    }
}
